package com.ezops.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ezops.dao.facade.IAssetDataTransferFacade;
import com.ezops.dao.facade.ITradeClientDataTransferFacade;
import com.ezops.dao.facade.ITradeExceptionFacade;
import com.ezops.dto.TradeExceptionDTO;
import com.ezops.models.TradeExceptions;

/**
 * 
 * @author dinesh
 * 
 */

@Controller
@RequestMapping("/exception")
public class ExceptionReportController {

	@Autowired
	private ITradeExceptionFacade tradeExceptionFacade;

	@Autowired
	private ITradeClientDataTransferFacade tradeClientFacade;

	@Autowired
	private IAssetDataTransferFacade assetFacade;

	final static Logger logger = LoggerFactory
			.getLogger(ExceptionReportController.class);

	/**
	 * Return the list of exception being generated for the particular client.
	 * 
	 * @param clientId
	 * @param assetClass
	 * @return
	 */

	@RequestMapping(value = "/getexceptionreport", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody
	List<TradeExceptionDTO> listExceptions(
			@RequestParam(value = "clientid", required = false, defaultValue = "1") Integer clientId) {
		List<TradeExceptions> models = tradeExceptionFacade.getExceptionByClient(clientId);

		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
		List<TradeExceptionDTO> dtos = new ArrayList<TradeExceptionDTO>();
		for (TradeExceptions exception : models) {
			// String errorMessae, String fieldName,
			// String fileName, String clientId, String action, int tradeId,
			// String assetId

			TradeExceptionDTO dto = new TradeExceptionDTO(
					exception.getErrorMessae(), exception.getFieldName(),
					exception.getFileName(),
					tradeClientFacade.reverseLookUp(exception.getClientId()),
					exception.getAction(), exception.getTradeId(),
					assetFacade.reverseLookUp(exception.getAssetId())
					);
			dto.setTimeStamp(dateFormat.format(exception.getCreatedDate()));
			dtos.add(dto);
		}

		return dtos;
	}
}
